package com.hzcx.app.simplechat.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hzcx.app.simplechat.ui.publicui.PublicConfirmDialog;
import com.hzcx.app.simplechat.ui.publicui.PublicTipsDialog;
import com.hzcx.app.simplechat.util.toast.ToastUtils;
import com.trello.rxlifecycle2.components.support.RxFragment;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

/**
 * fragment基类
 *
 * @param <T>
 */
public abstract class BaseFragment<T extends BasePresenter> extends RxFragment implements IView {

    public static final String TAG = BaseFragment.class.getSimpleName();

    protected T mPresenter;

    private BaseLoadingDialog loadingDialog;

    private View rootView;

    public Bundle savedInstanceState;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(rootView==null) {
            rootView = LayoutInflater.from(getActivity()).inflate(getLayout(), container, false);
            ButterKnife.bind(this, rootView);
        }
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.savedInstanceState=savedInstanceState;

        initPresenter();
        initView();
        initData();

        if (mPresenter != null) {
            mPresenter.attachView(this);
        }

        try {
            EventBus.getDefault().register(this);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public abstract int getLayout();

    public abstract void initPresenter();

    public abstract void initView();

    public abstract void initData();

    @Override
    public void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void showLoading() {
        try {
            loadingDialog = new BaseLoadingDialog(getActivity());
            loadingDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void hideLoading() {
        try {
            if (loadingDialog != null && loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void dataEmpty() {

    }

    @Override
    public void showError(String msg) {
        ToastUtils.show(msg);
    }

    /**
     *  通用的提示弹窗
     * @param title
     * @param content
     * @param listener
     */
    @Override
    public void showTips(String title, String content, PublicTipsDialog.OnPublicTipsClickListener listener) {
        PublicTipsDialog tipsDialog=new PublicTipsDialog(getActivity(),title,content,listener);
        tipsDialog.show();
    }

    /**
     *  通用的确认提示框
     * @param title
     * @param content
     * @param confirm
     * @param listener
     */
    public void showConfirmDialog(String title, String content, String confirm, PublicConfirmDialog.OnPublicConfirmClickListener listener){
        PublicConfirmDialog confirmDialog=new PublicConfirmDialog(getActivity(),title,content,confirm,listener);
        confirmDialog.show();
    }
}
